package org.as3search.criteria.impl {
	/*
	 * as3search - http://code.google.com/p/as3search
	 * Copyright (c) 2009-2010 by respective authors. All rights reserved.
	 *
	 * This library is free software; you can redistribute it and/or modify it under the
	 * terms of the GNU Lesser General Public License as published by the Free Software
	 * Foundation; either version 2.1 of the License, or (at your option) any later
	 * version.
	 *
	 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
	 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
	 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
	 *
	 * You should have received a copy of the GNU Lesser General Public License along
	 * with this library; if not, write to the Free Software Foundation, Inc.,
	 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
	 */
	
	import flash.errors.IllegalOperationError;
	
	import org.flexunit.Assert;
	
	import org.as3search.Search;
	import org.as3search.config.FlexSearchConfiguration;
	import org.as3search.config.SearchConfiguration;
	import org.as3search.criteria.Criterion;
	import org.as3search.criteria.EqualityExpression;
	import org.as3search.criteria.Restrictions;
	import org.as3search.providers.ArraySearchProvider;
	
	/**
	 * 
	 */
	public class SubCriteriaImplTestCase {
		
		private var classToTestRef:org.as3search.criteria.impl.SubCriteriaImpl;
		
		public function SubCriteriaImplTestCase() {
			
		}
		
		[Before]
		public function setup():void {
			classToTestRef = new SubCriteriaImpl("friends", Restrictions.conjunction()
				.add(Restrictions.eq("id", 0)), null, null);
		}
		
		[Test]
		public function testAdd():void {
			var config:SearchConfiguration = new FlexSearchConfiguration();
			classToTestRef = new SubCriteriaImpl("friends", Restrictions.eq("id", 0), config, null);
			
			var expr:EqualityExpression = new EqualityExpression("name", "fred");
			Assert.assertEquals("failed to return self",
				classToTestRef, classToTestRef.add(expr));
			Assert.assertTrue("failed to add to collection",
				classToTestRef.criteriaCollection.indexOf(expr) != -1);
			
			try {
				classToTestRef.add(expr);
				Assert.fail("failed to throw error on duplicate item add");
			} catch (e:ArgumentError) {
//				pass
			}
			
			try {
				classToTestRef.add(null);
				Assert.fail("failed to throw error on null item add");
			} catch (e1:ArgumentError) {
//				pass
			}
		}
		
		[Test]
		public function testConfiguration():void {
			Assert.assertNotNull("'configuration' should never be null!",
				classToTestRef.configuration);
		}
		
		[Test]
		public function testCreateCriteria():void {
			try {
				classToTestRef.createCriteria("abc", null);
				Assert.fail("failed to throw error on createCriteria");
			} catch (e:IllegalOperationError) {
//				pass
			}
		}
		
		[Test]
		public function testCriteriaCollection():void {
			Assert.assertNotNull("'criteriaCollection' should not be null",
				classToTestRef.criteriaCollection);
			classToTestRef.criteriaCollection = null;
			Assert.assertNotNull("failed to create an empty array when 'criteriaCollection' is assigned to null",
				classToTestRef.criteriaCollection);
		}
		
		[Test]
		public function testEvaluate():void {
			Assert.assertTrue("failed to correctly evaluate the collection (1 match)",
				classToTestRef.evaluate({friends:[{id:0},{id:1}]}));
			Assert.assertTrue("failed to correctly evaluate the collection (2 matches)",
				classToTestRef.evaluate({friends:[{id:0},{id:0},{id:1}]}));
			Assert.assertFalse("failed to correctly evaluate the collection (no matches)",
				classToTestRef.evaluate({friends:[{id:1},{id:2}]}));
		}
		
		[Test]
		public function testList():void {
			try {
				classToTestRef.list();
				Assert.fail("failed to throw error on list");
			} catch (e:IllegalOperationError) {
//				pass
			}
		}
		
		[Test]
		public function testParent():void {
			var parent:CriteriaImpl = new CriteriaImpl(new ArraySearchProvider([]), null);
			classToTestRef = new SubCriteriaImpl("friends", Restrictions.eq("id", 0), null, parent);
			Assert.assertEquals("failed to set 'parent' in constructor",
				parent, classToTestRef.parent);
		}
		
		[Test]
		public function testPropertyName():void {
			Assert.assertEquals("failed to set propertyName in constructor",
				"friends", classToTestRef.propertyName);
		}
		
		[Test]
		public function testSubCriteriaImpl():void {
			try {
				new SubCriteriaImpl(null, null, null, null);
				Assert.fail("failed to throw an error on a null property name");
			} catch (e1:ArgumentError) {
//				pass
			}
			
			try {
				new SubCriteriaImpl(" ", Restrictions.conjunction(), null, null);
				Assert.fail("failed to throw an error on an invalid property name");
			} catch (e2:ArgumentError) {
//				pass
			}
			
			var criterion:Criterion = Restrictions.conjunction();
			classToTestRef = new SubCriteriaImpl("propertyname", criterion, null, null);
			Assert.assertTrue("failed to add the criterion passed to the constructor",
				classToTestRef.criteriaCollection.indexOf(criterion) != -1);
			classToTestRef = new SubCriteriaImpl("propertyName", criterion, null, null);
			Assert.assertEquals("failed to load default configuration on null",
				Search.configuration, classToTestRef.configuration);
			var config:SearchConfiguration = new FlexSearchConfiguration();
			classToTestRef = new SubCriteriaImpl("propertyName", criterion, config, null);
			Assert.assertEquals("failed to set 'configuration' in constructor",
				config, classToTestRef.configuration);
		}
		
		[Test]
		public function testTest():void {
			try {
				classToTestRef.test();
				Assert.fail("failed to throw an error on test");
			} catch (e:IllegalOperationError) {
//				pass
			}
		}
		
		[Test]
		public function testUniqueResult():void {
			try {
				classToTestRef.uniqueResult();
				Assert.fail("failed to throw an error on uniqueResult");
			} catch (e:IllegalOperationError) {
//				pass
			}
		}
	}
}